package com.cup.bddm.api.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.cup.bddm.api.entity.MerchantEntity;
import com.cup.bddm.api.entity.StreetEntity;
import com.cup.bddm.api.req.StreetVo;
import com.cup.bddm.api.res.ResVo;
import com.cup.bddm.api.service.MerchantService;
import com.cup.bddm.api.service.StreetService;
import com.cup.bddm.config.Config;
import com.cup.bddm.handler.Token;

/**
 * 商圈
 */
@Controller
@RequestMapping(value = "/api/street", method = RequestMethod.POST)
public class StreetController extends BaseController {
	static Logger logger = Logger.getLogger(StreetController.class);

	@Autowired
	StreetService streetService;

	@Autowired
	MerchantService merchantService;

	/**
	 * 添加
	 */
	@Token
	@RequestMapping(value = "/add")
	@ResponseBody
	public ResVo add(@RequestBody StreetVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
		if (StringUtils.isEmpty(vo.getCup_branch_ins_id_cd())) {
			return the().cup_branch_ins_id_cd().is_empty().vo();
		}
		if (!Config.CUP_INS_ID.equals(vo.getCup_branch_ins_id_cd()) && !vo.getCup_branch_ins_id_cd().equals(user().getCup_branch_ins_id_cd())) {
			return the().cup_branch_ins_id_cd().is_invalid().vo();
		}
		if (StringUtils.isEmpty(vo.getStreet_cd())) {
			return the().street_cd().is_empty().vo();
		}
		if (StringUtils.isEmpty(vo.getStreet_nm())) {
			return the().street_nm().is_empty().vo();
		}
		StreetEntity street1 = streetService.oneByStreetCd(vo, true);
		if (street1 != null) {
			return the().Street().already_exist().eg(street1.getStreet_nm()).vo();
		}
		StreetEntity street2 = streetService.oneByStreetCdAndSrvMchntCd(vo, true);
		if (street2 != null) {
			return the().Street().SrvMchnt().already_exist().eg(street2.getService_mchnt_nm()).vo();
		}
		streetService.add(vo);
		return ResVo.ok();
	}

	/**
	 * 批量添加
	 */
	@Token
	@RequestMapping(value = "/add_batch")
	@ResponseBody
	public ResVo addBatch(@RequestBody List<StreetVo> list) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
		int ok = 0, up = 0;
		StringBuilder m = new StringBuilder();
		for (int i = 1; i <= list.size(); i++) {
			StreetVo vo = list.get(i - 1);
			if (StringUtils.isEmpty(vo.getCup_branch_ins_id_cd())) {
				m.append(i + ":cup_branch_ins_id_cd is empty\n");
				continue;
			}
			if (!Config.CUP_INS_ID.equals(vo.getCup_branch_ins_id_cd()) && !vo.getCup_branch_ins_id_cd().equals(user().getCup_branch_ins_id_cd())) {
				m.append(i + ":cup_branch_ins_id_cd is invalid\n");
				continue;
			}
			if (StringUtils.isEmpty(vo.getStreet_cd())) {
				m.append(i + ":street_cd is empty\n");
				continue;
			}
			if (StringUtils.isEmpty(vo.getStreet_nm())) {
				m.append(i + ":street_nm is empty\n");
				continue;
			}
			StreetEntity street = streetService.oneByStreetCdAndSrvMchntCd(vo, true);
			if (street != null) {
				if (streetService.modify(street, vo) > 0) {
					m.append(i + ":update ok\n");
					up++;
				}
			} else {
				if (streetService.add(vo) > 0) {
					m.append(i + ":add ok\n");
					ok++;
				}
			}
		}
		return ResVo.batch(ok, up, list.size() - ok - up, m.toString());
	}

	/**
	 * 查找-按名字/类型
	 */
	@Token
	@RequestMapping(value = "/find")
	@ResponseBody
	public ResVo find(@RequestBody StreetVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		return ResVo.data(streetService.findByVo(vo));
	}

	/**
	 * 修改
	 */
	@Token
	@RequestMapping(value = "/modify")
	@ResponseBody
	public ResVo modify(@RequestBody StreetVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		if (vo.invalidId()) {
			return the().id().is_invalid().vo();
		}
		if (StringUtils.isEmpty(vo.getStreet_cd())) {
			return the().street_cd().is_empty().vo();
		}
		StreetEntity entity = streetService.oneById(vo, true);
		if (entity == null) {
			return the().Street().do_not_exist().vo();
		}
		streetService.modify(entity, vo);
		return ResVo.ok();
	}

	/**
	 * 删除-假删除
	 */
	@Token
	@RequestMapping(value = "/delete")
	@ResponseBody
	public ResVo delete(@RequestBody StreetVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		if (vo.invalidId()) {
			return the().id().is_invalid().vo();
		}
		StreetEntity street = streetService.oneById(vo, true);
		if (street == null) {
			return the().Street().do_not_exist().vo();
		}
		// 是否有:相同商圈ID、不同服务商的数据
		vo.setStreet_cd(street.getStreet_cd());
		List<StreetEntity> streets = streetService.listByStreetCd(vo, true);
		if (streets.size() > 1) {
			streetService.setIsDel(street);
			return ResVo.ok();
		}
		// 商圈下是否有商户
		MerchantEntity merchant = merchantService.oneByStreetCd(street.getStreet_cd(), true);
		if (merchant != null) {
			return the().Street().has().Merchant().eg(merchant.getStore_nm()).vo();
		}
		streetService.setIsDel(street);
		return ResVo.ok();
	}

	/**
	 * 删除-真删除
	 */
	@Token
	@RequestMapping(value = "/delete_ever")
	@ResponseBody
	public ResVo delete_ever(@RequestBody StreetVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		if (vo.invalidId()) {
			return the().id().is_invalid().vo();
		}
		StreetEntity street = streetService.oneById(vo, false);
		if (street == null) {
			return the().Street().do_not_exist().vo();
		}
		// 是否有:相同商圈ID、不同服务商的数据
		vo.setStreet_cd(street.getStreet_cd());
		List<StreetEntity> streets = streetService.listByStreetCd(vo, true);
		if (streets.size() > 1) {
			streetService.delete(street);
			return ResVo.ok();
		}
		// 商圈下是否有商户
		MerchantEntity merchant = merchantService.oneByStreetCd(street.getStreet_cd(), false);
		if (merchant != null) {
			return the().Street().has().Merchant().eg(merchant.getStore_nm()).vo();
		}
		streetService.delete(street);
		return ResVo.ok();
	}

	/**
	 * 查找-列出分公司下所有商圈
	 */
	@RequestMapping(value = "/find/{cup_branch_ins_id_cd}", method = RequestMethod.GET)
	@ResponseBody
	public ResVo find_by_ins(@PathVariable String cup_branch_ins_id_cd) {
		if (StringUtils.isEmpty(cup_branch_ins_id_cd)) {
			return the().cup_branch_ins_id_cd().is_empty().vo();
		}
		return ResVo.data(streetService.findByIns(cup_branch_ins_id_cd));
	}

	/**
	 * 根据商圈ID前五位数，生成新的商圈ID
	 */
	@RequestMapping(value = "/serial/{biz_street_cd}", method = RequestMethod.GET)
	@ResponseBody
	public ResVo serial(@PathVariable String biz_street_cd) {
		if (biz_street_cd == null || biz_street_cd.length() != 5) {
			return the().biz_street_cd().is_invalid().vo();
		}
		String street_cd = streetService.getNewStreetCd(biz_street_cd.toUpperCase());
		return ResVo.data(street_cd);
	}
}
